//
//  VisitorLoginView.swift
//  微博
//
//  Created by 刘静 on 15/10/10.
//  Copyright © 2015年 liujing.cn. All rights reserved.
//

import UIKit

/// 访客视图协议
protocol VisitorLoginViewDelegate: NSObjectProtocol{

 /// 将要登录
    func visitorLoginViewWillLogin()
 /// 将要注册
    func visitorLoginViewWillRegister()


}
/// 访客视图
class VisitorLoginView: UIView {
    /// 定义代理 - 一定要用代理
    weak var delegate: VisitorLoginViewDelegate?
   
//MARK: -按钮监听方法
    func clickLogin() {
      delegate?.visitorLoginViewWillLogin()
        
    }
    
    func clickRegister(){
   delegate?.visitorLoginViewWillRegister()
    
    }
    //MARK:- 设置视图信息
    func setupViewInfo(isHome:Bool,imageName:String,message:String){
        messageLable.text = message
        iconView.image = UIImage(named: imageName)
        
        homeIconView.hidden = !isHome
    
        
        
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
        
       
        
           }
    
    ///  开始动画
    private func startAnimation(){
    
    let anim = CABasicAnimation(keyPath: "transform.rotation")
    
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    
    
    //MARK: - 界面初始化
    override init(frame: CGRect){
    
        super.init(frame: frame)
    
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        //禁止在 sb / xib使用本类
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setupUI()
        
    }
    
    
    //设置界面
    
    private func setupUI(){
       //1.添加控件
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
    //2.自动布局
        
        // NSLayoutConstraint  约束
      //默认在纯代码开发时，时可以使用setFrame 设置控件位置的
        //如果要使用自动布局，需要设置一个属性
        //一旦加入自动布局系统管理，就不要再设置frame
        
     //自动布局公式："view1.attr1 = view2.attr2 * multiplier（乘积） + constant(约束值)"
   //1>图标
    iconView.translatesAutoresizingMaskIntoConstraints = false
        
 
        //relatedBy  关系
       addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
       
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant:-40))
        //2>小房子
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
       
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
      
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))

        //3>信息文字

    messageLable.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    
         addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        //4>注册按钮
       registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        //5>登录按钮
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        //6>遮罩视图  VFL 可视化格式语言
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        
       addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":maskIconView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":maskIconView,"regButton": registerButton]))
        //7.设置背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
    }
    //MARK: - 懒加载控件
    
    //图标
    
    private lazy var iconView: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return iv
    }()
    
    //遮罩视图
    private lazy var maskIconView: UIImageView = {
    
        
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv

        
    }()
    //小房子
    private lazy var homeIconView:UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return iv
    
    }()
    
///  消息文字 
    private lazy var messageLable: UILabel = {
    
    
    let lable = UILabel()
        
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
    
        lable.textColor = UIColor.darkGrayColor()
        
        lable.font = UIFont.systemFontOfSize(14)
        
        lable.numberOfLines = 0
        
        lable.sizeToFit()
        
        return lable
        
    }()
    
///  注册按钮
    private lazy var registerButton: UIButton = {
    
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
    
    return btn
    
    
    }()
    
///  登录按钮
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
        }()
 


}
